import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-03-15
 * Time:22:21
 */
public class TestjdbcUpdate {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        Connection connection = dataSource.getConnection();

        //String sql = "update student set name = '女孩' where id = '1234'";
        String sql = "update student set name = ? where id = ?";
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要修改的名字:");
        String name = input.next();
        System.out.println("请输入要修改人的id:");
        String id = input.next();

        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setString(2,id);
        System.out.println("statement = " + statement);

        int a =  statement.executeUpdate();
        System.out.println(a);

        statement.close();
        connection.close();
    }
}
